//
//  GQDiscoverController.m
//  GuQu
//
//  Created by HELIUM STAR on 15/5/7.
//  Copyright (c) 2015年 HELIUM STAR. All rights reserved.
//

#import "GQDiscoverController.h"
#import "GQDiscoverTitleView.h"
#import "GQInfoController.h"
#import "GQCityController.h"
#import "GQFameHallController.h"

@interface GQDiscoverController () <GQDiscoverTitleViewDelegate, UIScrollViewDelegate>

@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) GQDiscoverTitleView *titleView;
@property (assign, nonatomic) NSInteger index;

@end

@implementation GQDiscoverController

- (void)viewDidLoad {
    [super viewDidLoad];
    GQDiscoverTitleView *GQDT = [[GQDiscoverTitleView alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    GQDT.delegate = self;
    self.titleView = GQDT;
    GQDT.item = @[@"资讯", @"同城", @"名人堂"];
    self.navigationItem.titleView = GQDT;
    
    //创建UIScrollView
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kToolBarHeight)];
    scrollView.contentSize = CGSizeMake(kScreenWidth * GQDT.item.count, 0);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.bounces = NO;
    scrollView.delegate = self;
    //scrollView.contentOffset = CGPointMake(0, 0);
    scrollView.pagingEnabled = YES;
    scrollView.backgroundColor = [UIColor purpleColor];
    self.scrollView = scrollView;
    [self.view addSubview:scrollView];
    
    //创建自控制器
    GQInfoController *GQIC = [[GQInfoController alloc] init];
    GQIC.view.frame = CGRectMake(0, 0, scrollView.width, scrollView.height);
    
    GQCityController *GQCC = [[GQCityController alloc] init];
    GQCC.view.frame = CGRectMake(scrollView.width, 0, scrollView.width, scrollView.height);
    
    GQFameHallController *GQFHC = [[GQFameHallController alloc] init];
    GQFHC.view.frame = CGRectMake(scrollView.width * 2, 0, scrollView.width, scrollView.height);
    
    [scrollView addSubview:GQIC.view];
    [scrollView addSubview:GQCC.view];
    [scrollView addSubview:GQFHC.view];
    [self.view addSubview:scrollView];
    [self addChildViewController:GQIC];
    [self addChildViewController:GQCC];
    [self addChildViewController:GQFHC];
    
    
}

- (void)discoverTitleView:(GQDiscoverTitleView *)titleView didClick:(NSInteger)index
{
    self.scrollView.contentOffset = CGPointMake(kScreenWidth * index, 0);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger pageIndex = scrollView.contentOffset.x / kScreenWidth + 0.5;
    if (_index != pageIndex) {
        [self.titleView setSelectedIndex:pageIndex];
        _index = pageIndex;
    }
}

@end
